<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-form ref="queryFormData" :model="queryFormData" inline size="small">
          <el-button type="primary" size="small" :disabled="multipleSelection.length !== 0 ? false : true"
                     icon="el-icon-delete" @click="delAll">批量失效
          </el-button>
          <el-form-item label="歌手" style="margin-left: 10px">
            <el-input v-model="queryFormData.name" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="queryFormData.gender" style="width: 150px">
              <el-option value="" label="全部"/>
              <el-option :value="0" label="男"/>
              <el-option :value="1" label="女"/>
              <el-option :value="2" label="组合"/>
              <el-option :value="3" label="不明"/>
            </el-select>
          </el-form-item>
          <el-form-item label="地区">
            <el-select v-model="queryFormData.loc" style="width: 150px">
              <el-option value="" label="全部"/>
              <el-option :value="0" label="内地"/>
              <el-option :value="1" label="港台"/>
              <el-option :value="2" label="欧美"/>
              <el-option :value="3" label="韩国"/>
              <el-option :value="4" label="日本"/>
            </el-select>
          </el-form-item>
          <el-form-item label="有效性">
            <el-select v-model="queryFormData.dataFlag" style="width: 150px">
              <el-option value="" label="全部"/>
              <el-option :value="0" label="有效"/>
              <el-option :value="1" label="无效"/>
            </el-select>
          </el-form-item>
          <el-button size="small" icon="el-icon-search" @click="search">搜索</el-button>
          <el-button type="primary" size="small" icon="el-icon-circle-plus-outline"
                     @click="add">添加歌手
          </el-button>
        </el-form>
      </div>
      <el-table v-loading="loading" :data="tableData" size="mini" style="width: 100%"
                @selection-change="handleSelectChanged">
        <el-table-column type="selection" :selectable="selectable"/>
        <el-table-column label="歌手图片" width="110" align="center">
          <template slot-scope="{row}">
            <div class="singer-img">
              <img :src="row.pic" style="width: 100%; border-radius: 50%;"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="歌手" width="110" align="center"/>
        <el-table-column prop="genderDesc" label="性别" width="110" align="center"/>
        <el-table-column prop="bir" label="生日" width="110" align="center"/>
        <el-table-column prop="locDesc" label="地区" width="110" align="center"/>
        <el-table-column prop="intro" label="简介" align="center">
          <template slot-scope="{row}">
            <p style="height: 100px; overflow: scroll">{{ row.intro }}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" label="歌曲" prop="song" width="100">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.dataFlag === 0 ? false : true" @click="songManage(row.id)"
                       icon="el-icon-headset">歌曲
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" prop="operation" width="150">
          <template slot-scope="{row}">
            <el-button size="mini" @click="edit(row.id)">编辑</el-button>
            <el-button size="mini" :disabled="row.dataFlag === 0 ? false : true" type="danger"
                       @click="move(row.id)">失效
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next, total"
                       :current-page="pageNum" :page-size="pageSize" :total="total"/>
      </div>
    </div>
    <el-dialog :title="isAdd ? '添加歌手' : '编辑歌手'" v-if="centerDialogVisible" :visible.sync="centerDialogVisible"
               width="500px" @close="handleCancel" center :close-on-click-modal="false">
      <el-form :model="registerForm" ref="registerForm" label-width="70px" :rules="formRules" size="mini"
               v-loading="loading" label-position="left">
        <el-form-item label="歌手名" prop="name" class="is-required">
          <el-input v-model="registerForm.name" clearable/>
        </el-form-item>
        <el-form-item label="图片地址" prop="pic">
          <el-input type="textarea" v-model="registerForm.pic"/>
        </el-form-item>
        <el-form-item label="性别" prop="gender" class="is-required">
          <el-radio-group v-model="registerForm.gender">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
            <el-radio :label="2">组合</el-radio>
            <el-radio :label="3">不明</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="registerForm.gender != 2" label="生日" prop="bir" class="is-required">
          <el-date-picker type="date" v-model="registerForm.bir" value-format="yyyy-MM-dd"
                          style="width: 100%"/>
        </el-form-item>
        <el-form-item label="地区" prop="loc" class="is-required">
          <el-radio-group v-model="registerForm.loc">
            <el-radio :label="0">内地</el-radio>
            <el-radio :label="1">港台</el-radio>
            <el-radio :label="2">欧美</el-radio>
            <el-radio :label="3">韩国</el-radio>
            <el-radio :label="4">日本</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="简介" prop="intro" class="is-required">
          <el-input type="textarea" v-model="registerForm.intro"/>
        </el-form-item>
        <el-form-item label="有效性" prop="dataFlag" class="is-required">
          <el-switch v-model="registerForm.dataFlag" :active-value="0" :inactive-value="1" active-text="生效"
                     inactive-text="失效"/>
        </el-form-item>
      </el-form>
      <span slot="footer">
                <el-button size="mini" @click="handleCancel">取消</el-button>
                <el-button type="primary" size="mini" @click="addSinger">确定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  mixin
} from '../mixins';
import {
  listSinger,
  getSinger,
  saveSinger,
  delSinger
} from '../api/index';

export default {
  mixins: [mixin],
  data() {
    return {
      centerDialogVisible: false,
      isAdd: false,
      registerForm: {
        name: '',
        gender: 0,
        bir: '',
        loc: 0,
        intro: '',
        dataFlag: 0,
        pic: ''
      },
      formRules: {
        name: [{
          required: true,
          message: '请输入歌手名',
          trigger: 'blur'
        }],
        bir: [{
          required: true,
          message: '请选择日期',
          trigger: 'blur'
        }],
        intro: [{
          required: true,
          message: '请输入简介',
          trigger: 'blur'
        }],
      },
      tableData: [],
      queryFormData: {
        name: '',
        gender: '',
        loc: '',
        dataFlag: ''
      },
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loading: true,
      multipleSelection: [],
    }
  },
  methods: {
    getData() {
      this.loading = true;
      const {
        queryFormData,
        pageNum,
        pageSize
      } = this;
      listSinger(queryFormData, pageNum, pageSize).then(res => {
        this.loading = false;
        const {
          data,
          total,
          success
        } = res;
        if (!success) {
          this.$message.error('查询失败');
          this.tableData = [];
          return;
        }
        this.tableData = data;
        this.total = total;
      })
    },
    addSinger() {
      this.$refs.registerForm.validate(isValid => {
        if (isValid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          if (this.registerForm.gender === 2) {
            this.registerForm.bir = "";
          } else {
            this.registerForm.bir = JSON.parse(JSON.stringify(this.dayjs(this.registerForm.bir)
                .format("YYYY-MM-DD 00:00:00")));
          }
          saveSinger(this.registerForm).then(res => {
            loading.close();
            const {
              success
            } = res;
            if (success) {
              this.$message.success("保存成功");
              this.centerDialogVisible = false;
              this.getData();
            } else {
              this.$message.error("保存失败");
            }
          });
        }
      });
    },
    handleCurrentChange(currentPageNum) {
      this.pageNum = currentPageNum;
      this.getData();
    },
    search() {
      this.getData();
    },
    del(ids) {
      delSinger(ids).then(res => {
        const {
          success
        } = res;
        if (success) {
          this.$message.success("操作成功");
          this.getData();
        } else {
          this.$message.error("操作失败");
        }
      });
    },
    move(id) {
      this.$confirm('确定失效该记录吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = [id];
        this.del(ids);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        });
      });
    },
    edit(id) {
      this.isAdd = false;
      this.centerDialogVisible = true;
      this.loading = true;
      getSinger(id).then(res => {
        const {
          data,
          success
        } = res;
        if (success) {
          this.loading = false;
          this.registerForm = {
            ...data
          };
        }
      })
    },
    handleCancel() {
      this.centerDialogVisible = false;
      let obj = {
        name: '',
        gender: 0,
        bir: '',
        loc: 0,
        intro: '',
        dataFlag: 0,
        pic: ""
      };
      this.registerForm = JSON.parse(JSON.stringify(obj));
    },
    add() {
      this.isAdd = true;
      this.centerDialogVisible = true;
      let obj = {
        name: '',
        gender: 0,
        bir: '',
        loc: 0,
        intro: '',
        dataFlag: 0,
        pic: ""
      };
      this.registerForm = JSON.parse(JSON.stringify(obj));
    },
    songManage(id) {
      this.$router.push({
        path: "/Song",
        query: {
          singerId: id
        }
      });
    },
    delAll() {
      this.$confirm('确定批量失效吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = this.multipleSelection.map(e => e.id);
        this.del(ids);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        });
      });
    },
    selectable(row) {
      return row.dataFlag === 1 ? false : true;
    }
  },
  mounted() {
    this.getData();
  }
}
</script>

<style scoped>
.singer-img {
  width: 100%;
  height: 90px;
  border-radius: 5px;
  margin-top: 5px;
  margin-bottom: 5px;
  overflow: hidden;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
</style>